uploading image in swift with multiple parameters

后端 未结 1 1087
难免孤独
难免孤独 2021-01-16 00:26

I am trying to upload an image to a backend client using swift. Trouble is I can\'t seem to get the formatting correct for the httpbody. I do not want to use a multipart for

相关标签:
1条回答
  • 2021-01-16 00:40

    You cannot post binary data in a application/x-www-form-urlencoded request like this. Actually, the code in your question looks like it will try to send a hexadecimal string representation of the binary data, which, probably is not what you intended and even if you did intend to do that, (a) you would have to decode it somehow on the server side; (b) note that this is very inefficient (more than doubles the size of the image payload): and (c) would need to be percent escaped in the request. But I don't think you intended that at all, anyway, so that is probably all moot.

    One would generally either create multipart/form-data request as outlined here (in which the uploaded file comes in as a file, e.g. $_FILES in PHP), or one would convert this binary data to text (e.g. using base64) and the the server code has convert the base64 value for image_data key back to binary data.

    By the way, I might suggest Alamofire or AFNetworking as alternatives to trying to create requests properly. It doesn't change the underlying issue (you have to pick between a base64 encoding or multipart requests), but it simplifies the Swift code.

    0 讨论(0)
提交回复
热议问题