Upload Photo / File with JSON and custom headers via Swift 3 and Alamofire 4 | iOS | Swift

后端 未结 3 1854
轻奢々
轻奢々 2020-12-31 18:54

I need to call the Multipart request with Image file and JSON.

I have tried this, but still getting the error.

 // define parameters
  let parameters         


        
3条回答
  •  执念已碎
    2020-12-31 19:34

    From data you have given, I comes to an conclusion as given below.

    The status code 400 means json you're passing to the api was not following the api documentation. For example if they expect a key "name" and you're not given such a key at all.

    Other possible reasons for this error.

    Seems like you haven't mentioned any of content types

    Add these line of code to assure that the response and your request are in proper formats

    Alamofire.request(.GET, "your url", parameters: ["foo": "bar"])
             .validate(statusCode: 200..<300)
             .validate(contentType: ["application/json"])
             .response { (_, _, _, error) in
                      println(error)
             }
    

    The Accept header tells the server what your client wants in the response. The Content-Type header tells the server what the client sends in the request.

    If you can give more information we can help further.

提交回复
热议问题