-1

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.

Eduard
  • 516
  • 1
  • 4
  • 17
  • If curl works and your application doesn't, then obviously the request isn't a post request. You may stick to this example here: https://stackoverflow.com/a/32682006/4934937 – maio290 Jan 13 '20 at 02:27
  • can you show the code of your request??? – Andres Gomez Jan 13 '20 at 02:28
  • 404 means file not found. "https://website.com/api/v1/subscribe" is therefore wrong/not existent/misspelled. – Melvin Jan 13 '20 at 02:38

1 Answers1

0

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.

Eduard
  • 516
  • 1
  • 4
  • 17