Warm tip: This article is reproduced from stackoverflow.com, please click
post swift nsurlsession urlsession

request.httpMethod == "POST" but backend receives GET instead ????

发布于 2020-03-31 22:53:08

I use standard URLSession to send a POST request

URLSession(configuration: .default).dataTask(with: urlRequest)

but it returns a backend error

{
    "http_code" = 404;
    message = "Not Found";
}

I told a backend guy about it and he said that the request fails because the server somehow receives my request as GET

136.*.*.228 - - [02/Dec/2019:09:01:48 +0000] "GET /api/v1/subscribe HTTP/2.0" 404 49 "-" "App/16 CFNetwork/1120 Darwin/18.7.0" "-"

but print(urlRequest.httpMethod) right before dataTask(with: urlRequest) shows "POST"

A request from the Terminal works just fine

curl -X POST "https://website.com/api/v1/subscribe" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"email\": \"email@mail.com\", \"message\": \"hey\", \"name\": \"John Doe\", \"tag\": \"preorder\"}" 

EDIT: Don't want to be rude, but did anyone try reading the post before answering with standard phrases? I know what 404 means. I know that the path is correct.

Questioner
Eduard
Viewed
98
Eduard 2020-01-31 18:37

I removed "www." from the request and the error changed to the right one — 400 (instead of 404). It appeared that we had a redirect from www.sitename.com to sitename.com and I didn't know about it.

Posting it in case anybody encounters the same unexpected problem.