I'm trying to upload a multipart file as JSON encoded, rather than URL encoded. It seems to default to URL encoded, but I can't figure out a way to change this. With other Alamofire methods you can explicitly state the encoding like:
Alamofire.request(_, _, parameters: _, encoding: .JSON)
Is there a similar way I can change the encoding on the Alamofire upload function?
Related
I would like to do a REST resource that could be represented as:
PDF (binary for download);
JSON (with structured data);
PDF (base64 encode)
My expectation is to create a URL like this:
http://api.mybank.com/accounts/{accountId}/statement?fromDate=2019-11
The Accept header as 'application/json' will return the statement information.
The Accept header as 'application/pdf' will return the statement in a PDF binary file.
And PDF encoded as base64? How I define this?
Content-Transfer-Encoding is not an HTTP, but email header.
Transfer-Encoding is used only for compression.
I am getting Korean data from the api server through alamofire. However, the data I receive breaks as below.
ëë¶ìë°©ì
where in the middle it should be korean but returns unrecognizable symbols. How do I encode this data to be korean again?
this is the code I use in alamofire
Alamofire.request(BusURL + "?\(urlParams)", method: .get).validate()
.responseString {}
Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: [content-type: multipart/form-data])
In header of request I see that there a field called content-type which can be set as application/json, application/x-www-form-urlencoded, multipart/form-data. But the problem is that in encoding param we can also set as JSONEncoding.default. Then what are some differences between encoding vs content-type in Alamofire.
encoding: for parameter which we sending to server
content-type: for specifying the response type
The encoding parameter sets how should Alamofire parse the passed parameters. For example, if it's set to URLEncoding.default, Alamofire will append the parameters to the URL.
The Content-Type header, on the other hand, tells the web server what type of content do we want to semd.
From the MDN web docs:
The Content-Type entity header is used to indicate the media type of the resource.
In responses, a Content-Type header tells the client what the content type of the returned content actually is.
Since Alamofire is an HTTP request wrapper, it provides a parameter for settings HTTP headers. As described by W3C, HTTP headers define how your request will be handled by server : W3C' Header Field Definitions.
An additional encoding parameter is available in Alamofire since you have to describe how to format an input response. Briefly, server cannot understand how to process a Swift Array or Dictionary, you need to explicitly write what kind of encoding is required. Alamofire provides several solutions to encode Swift object : Alamofire Encoding Options.
In this way, according to your request method (PUT, GET, POST, ...) you could have an Header ["Accept": "application/json"] and differents Alamofire encoding options.
I am sending data in JSON with content-type JSON but this shows me the content-type XML.
So the server could not read my request.
Also, that is the post request
The problem is that you are using .responseJSON which tells Alamofire that the response would contain JSON. Since the response is XML and not JSON in your case, Alamofire would throw an error. What you need instead is not to specify the response type and then an XMLParser to parse the data. One option is SWXMLHash. Your updated code would look something like this
Alamofire.request(request)
.response { response in
var xml = SWXMLHash.parse(response.data!)
}
Basically, this is not an error of content-type. this is an error of data type.
I send the all value in the string but there is need to send the data with data type.
When I request with data type it automatically changed content-type in JSON.
I have looked at questions like this or that one but it is still not working.
Also I have a question about what should I enter into fileURL param from function multipartFormData.appendBodyPart?
Should it be a way to image from PC, or image must be added to Images.xcassets? What should I send here?
It looks like you have three issues that you need to fix.
Use .POST instead of POST.
The fileURL needs to be a valid NSURL that points to a file on the file system. You cannot just use the filename.
You are using the responseString serializer, but named the third parameter in the closure JSON. Then you are letting result into s and trying to print it out. The result parameter doesn't even exist anywhere. Instead, you should print(JSON).
Hopefully that helps clear things up a bit.
Try to use .POST not POST
As an alternative solution upload an encoded file and send it as a parameter of POST.
// `data` is NSData
let base64String = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
let parameters = ["image_data": base64String] as [String: AnyObject]
Alamofire.request(.POST, "http://your-url.com", parameters: parameters)
The cons of this method is that the data will get %33 larger due to encoding. If you have bandwidth problems it may not be a good solution.