Fshamri Fshamri - 6 months ago 83
PHP Question

How to stimulate cURL request to a request using postman

I am using the following cURL request to localhost which runs fine:

curl -u admin:e4d4face52f2e3dc22b43b2145ed7c58ce66e26b384d73592c -d "{\"jsonrpc\": \"2.0\", \"method\": \"feed.list\", \"id\": 1}" http://localhost/minifluxR/jsonrpc.php


But when I send the same request using Postman instead of cURL, I am getting:

{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}


In Postman I used a
GET
request and sent the following as headers:

url:http://localhost/minifluxR/jsonrpc.php
username:admin
api_token:e4d4face52f2e3dc22b43b2145ed7c58ce66e26b384d73592c
method: feed.list


The following is the PHP function I am trying to trigger:

$server = new Server;

$server->authentication(array(
\Model\Config\get('username') => \Model\Config\get('api_token')
));

// Get all feeds
$server->register('feed.list', function () {
return Model\Feed\get_all();
});


Please help me to correct these errors.

Answer

When using cURL, the -u option (or --user) is used to supply the credentials for HTTP Basic authentication. This sets the Authorization header to contain the necessary data to authenticate with the server.

These steps apply to Postman's packaged app. For steps for the legacy app, view this of revision this answer.

To use HTTP Basic authentication as you were in your cURL command, click the Authorization tab and enter your credentials. Clicking Update Request will add the necessary Authorization header for you.


Postman's Authorization tab


Postman's Headers tab


To submit the JSON data in the same way that you did with cURL, use a POST request, select raw under the Body tab, and enter your data like so:


Postman's Body tab


To debug this I used Fiddler - a free web debugging proxy.

I used cURL's --proxy option to make it send its requests through Fiddler like so:

curl \
--proxy http://localhost:8888 \
-u foo:bar \
-d "{\"jsonrpc\": \"2.0\", \"method\": \"feed.list\", \"id\": 1}" \
http://localhost

Now that the request goes through Fiddler, I can select it from the session list, and use the "raw" inspector to see the raw request:


Fiddler's "raw" inspector


This shows me that the cURL is making a POST request with HTTP Basic authentication and application/x-www-form-urlencoded content. This type of data normally consists of keys and values, such as foo=bar&hoge=fuga. However, this cURL request is submitting a key without a value. A call to var_dump($_POST) will yield the following:


var_dump output


With a = at the end of the data (like so: {"jsonrpc": "2.0", "method": "feed.list", "id": 1}=) the var_dump will yield the following:


var_dump output


However, it seems that JsonRPC will use file_get_contents('php://input') in your case. This returns the data that was submitted with the request, including a =, if the data ends with it. Because it will try to parse the input data as a JSON string, it will fail if the string ends with a =, because that would be invalid JSON.

Using the FoxyProxy extension for Chrome, I created a proxy configuration for Fiddler (127.0.0.1:8888), which allowed me to easily debug the data being sent by Postman's POST request. Using x-www-form-urlencoded with a key of foo with no value, the data sent was actually foo=, which would result in your JSON string being invalid.

However, using "raw" input will allow for the specified data to be sent without a = being added to the end of it, thus ensuring the data is valid JSON.

Comments