0

I am trying to send POST data using AFNetworking 2.0 using the code:

NSDictionary *loginParameters = @{@"secretKey": @"SECRET_KEY", @"userOrEmail": username, @"password": password};

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager POST:@"https://api.moj.io/v1/login/APP_ID" parameters:loginParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"object is %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failure message is %@", [error localizedDescription]);
}];

This keeps giving me a response saying Request failed: not found (404)

But if I use the Postman REST client (on the chrome browser), using the URL https://api.moj.io/v1/login/APP_ID and the parameters for secretKey, userOrEmail and password, it gives the correct response.

What is the mistake I am making ?

Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125

3 Answers3

0

This URL is not correct?

https://api.moj.io/v1/login/APP_ID

Replace APP_ID with your app_id (maybe you are showing sample code here?)

Also most likely is that the device or simulator you are testing does not have internet access? Have you tried browsing to this site in the simulator and on device with this URL?

I use "Charles" all the time, but you can get this HTTP and HTTPS proxy application to monitor all the traffic coming from the iPhone simulator so you can actually see if the application is making this HTTPS request. I have been using Charles for years and it saves me so much time. http://www.charlesproxy.com/documentation/faqs/ssl-connections-from-within-iphone-applications/

Your code looks fine, you are doing a "POST" to this url as well. Vs a "GET". When you do this by pasting in your browser address bar. You are actually doing a GET. So check your documentation to make sure you should be doing POST or GET?

John Ballinger
  • 7,380
  • 5
  • 41
  • 51
0

Error is 404 not found, so something wrong is from your side, you're trying to access particular part of your web service which is not found on server.

Without original URL no one would give you what the wrong you're doing. But in my good I can say you should check,

  1. URL is proper or not
  2. Confirm the API (service) which you've to access
  3. Double check parameters which you're passing, if SECRET_KEY or userOrEmail.
  4. If you're getting this inside failure block then most probably first three point should solve your problem.

As you said, this is working fine in POSTMAN but not in code then you can ask your API developers to return everything which you're passing, in that way you can make sure that your information is correct or not.

PS. Few days back, I had the same issue where API was working fine in POSTMAN and in browser, and even iPhone's Safari browser too. But not with code. There was no problem in code, then I asked them to return me everything which I'm passing them, and strangely the problem was inside the API, a query argument was become empty (yeah, it was very very strange!) but this is how I solved that.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • hey, @Hemang sir ... please, can you solve this issue ... http://stackoverflow.com/questions/37345790/request-failed-bad-request-400-in-afmultipartformdata/37363517?noredirect=1#comment62284820_37363517 – Suraj Sukale May 24 '16 at 05:23
-1

It maybe a bug in the framework, because even the example code is also fail with domain error:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)"

John Ballinger
  • 7,380
  • 5
  • 41
  • 51
Pandara
  • 139
  • 7
  • If you actually look at that URL this resource is NOT returning JSON content. The HTTP headers being returned are (using charles): HTTP/1.1 404 Not Found Accept-Ranges: bytes Cache-Control: max-age=604800 Content-Type: text/html Date: Thu, 16 Oct 2014 09:40:45 GMT Etag: "359670651" Expires: Thu, 23 Oct 2014 09:40:45 GMT Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT Server: ECS (rhv/8195) X-Cache: 404-HIT x-ec-custom-error: 1 Content-Length: 1270 – John Ballinger Oct 16 '14 at 09:42
  • I dont really understand what you mean :( – Pandara Oct 16 '14 at 10:09