Search query works in browser but error out in curl method

1.1k views Asked by At

The following search query works in any internet browser, like IE, Chrome or Firefox.

http://search.mobile.walmart.com/search?query=bread&store=5461&size=20&offset=0

But, if I use curl POST method with parameters:

curl -d "query=bread&store=5461&size=20&offset=0" -X POST http://search.mobile.walmart.com/search

I got error message:

"StatusCode:404, "error":"Not Found"

2

There are 2 answers

0
Dmitry On

If it works in a browser then it's a GET method not a POST method. But if you run it with GET like this: curl -d "query=bread&store=5461&size=20&offset=0" -X GET http://search.mobile.walmart.com/search . You'll get the following:

<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access "http&#58;&#47;&#47;search&#46;mobile&#46;walmart&#46;com&#47;search" on this server.<P>
Reference&#32;&#35;18&#46;2d00a81f&#46;1504771199&#46;d72c55
</BODY>
</HTML>

Which basically means Walmart does not want you to access its website using anything other then a web browser. But you still can try changing the headers to mimic a browser. Also change the UserAgent string. Look in this answer on how to do it. This might help.

EDIT Actually I've checked it right now and using just curl -X GET "http://search.mobile.walmart.com/search?query=bread&store=5461&size=20&offset=0" works fine. You dont need to use -d with GET. Just add the query string to the URL.

0
hanshenrik On

you're doing 2 mistakes here, first off, that url does not support POST requests, by the looks of it, it only supports GET requests, so don't do -X POST, second, the website does not support having the search parameters in the request body, and -d put the data in the request body, in your example, with the application/x-www-urlencoded format. this site only supports having the parameters in the request URL by the looks of it. so do

curl 'http://search.mobile.walmart.com/search?query=bread&store=5461&size=20&offset=0'

this will issue a GET request, with the parameters in the url, just like the website wants. protip, when debugging curl, add the -v parameter, it prints lots of useful debugging info :)