Closed petyots closed 1 year ago
Hello @petyots, I think the problem is that you try to insert nil conditionally as avatar parameter. The following code filters out any nil value from the params.
case .register(let verificationId, let verificationCode, let mobilePhone, let deviceName, let userModel, let avatar):
return .init(method: .post,
path: .path(["auth", "register"]),
params: ["mobile_phone": mobilePhone,
"verification_id": verificationId,
"verification_code": verificationCode,
"device_id": deviceName,
"first_name": userModel.firstName,
"last_name": userModel.lastName,
"username": userModel.username,
"avatar": avatar != nil ? MultipartFormDataPartType.url(avatar!) : nil
].compactMapValues { $0 }
)
Please let me know if it works.
@billp Thanks for your time.
Unfortunately no joy...
2023-08-14 23:15:17.978950+0300 BULLTIME[54434:16877627] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'
case .register(let verificationId, let verificationCode, let mobilePhone, let deviceName, let userModel, let avatar):
let config = Configuration()
//config.requestBodyType = .multipartFormData(boundary: UUID().uuidString)
//config.verbose = true
return .init(method: .post,
path: .path(["auth", "register"]),
params: ["mobile_phone": mobilePhone,
"verification_id": verificationId,
"verification_code": verificationCode,
"device_id": deviceName,
"first_name": userModel.firstName,
"last_name": userModel.lastName,
"username": userModel.username,
"avatar": avatar != nil ? MultipartFormDataPartType.url(avatar!) : nil
].compactMapValues { $0 },
configuration: config
)
If I avatar is nil it's fine but when I pass it down it throws
@petyots Sorry, I forgot to add the MultipartFormDataPartType.value to the rest of the params. The point is that when calling upload you should use either MultipartFormDataPartType.value, MultipartFormDataPartType.url or MultipartFormDataPartType.data, otherwise it doesn't now how to append it to upload body. Try this, it should work:
case .register(let verificationId, let verificationCode, let mobilePhone, let deviceName, let userModel, let avatar):
let config = Configuration()
//config.requestBodyType = .multipartFormData(boundary: UUID().uuidString)
//config.verbose = true
return .init(method: .post,
path: .path(["auth", "register"]),
params: ["mobile_phone": MultipartFormDataPartType.value(mobilePhone),
"verification_id": MultipartFormDataPartType.value(verificationId),
"verification_code": MultipartFormDataPartType.value(verificationCode),
"device_id": MultipartFormDataPartType.value(deviceName),
"first_name": MultipartFormDataPartType.value(userModel.firstName),
"last_name": MultipartFormDataPartType.value(userModel.lastName),
"username": MultipartFormDataPartType.value(userModel.username),
"avatar": avatar != nil ? MultipartFormDataPartType.url(avatar!) : nil
].compactMapValues { $0 },
configuration: config
)
I will think about getting rid of MultipartFormDataPartType.[type] in a future release and use the value directly.
Also I think you don't need the config param. It will inherit it from parent.
Does it work now?
@billp 1000x It's working now as it should. Thanks again!. Maybe we can add some useful docs? What do you think
You're welcome @petyots. Yes sure, I will add the usage details in README in the next days. Thank you!
Hi, I am trying to upload a simple png file without success. The request is throwing thread exception because of wrong body type I think.
The endpoint configuration in my repo:
This is the caller
And finally the exception thrown
I am thinking it's because of wrong body type since the request is trying to write json output while the avatar param is MultiPart
Once this is resolved I will try to find time to create PR with documentation on uploading
EDIT:
By explicitly configuring the body as multipart its working fine. But if verbose is enabled it does not work in either way so I think the issue is in asyncUpload() not setting the appropriate body type.
EDIT 1:
When no file is passed even though the body is same as with avatar the request is empty.