How to stimulate a cURL request for a request using the mail manager

I use the following cURL request for localhost, which works 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 get:

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

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

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

The following is the PHP function I'm trying to call:

 $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 fix these errors.

+8
php curl
source share
2 answers

When using cURL, the -u (or --user ) --user used to provide credentials for --user HTTP authentication . This sets the Authorization header, which contains the necessary information for authentication with the server.

These steps apply to the Postman packaged application . For steps for an outdated application, check out this version of this answer.

To use basic HTTP authentication as you were in your cURL command, go to the Authorization tab and enter your credentials. By clicking Update Request , you will add the necessary Authorization header for you.


Postman's authorization tab


Postman's headers tab


To send JSON data in the same way as with cURL, use the POST request, select raw on the Body tab and enter your data as follows:


Postman's body tab


To debug this, I used Fiddler , a free proxy server for web debugging.

I used the cURL parameter --proxy so that it sends its requests via Fiddler as follows:

 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 list of sessions and use the β€œraw” inspector to view the raw request:


Fiddler's "raw" inspector


This shows me that cURL makes a POST request with basic HTTP validation and the contents of application/x-www-form-urlencoded . This data type usually consists of keys and values, such as foo=bar&hoge=fuga . However, this cURL request passes a key without a value. Calling var_dump($_POST) will result in the following:


var_dump output


C = at the end of the data (for example: {"jsonrpc": "2.0", "method": "feed.list", "id": 1}= ) var_dump will give the following:


var_dump output


However, it looks like JsonRPC will use file_get_contents('php://input') in your case. This returns the data that was sent with the request, including = if the data ends with it. Since it will try to parse the input as a JSON string, it will fail if the string terminates with = , because it will 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 sent by POST . Using x-www-form-urlencoded with the foo key with no value, the data sent was actually foo= , which would invalidate the JSON string.

However, using raw input will allow you to send the specified data without adding = to the end, thereby ensuring the validity of the JSON data.

+21
source share

Curl by default uses HTTP authentication by default. Your Postman titles are something else. Try using Basic Auth in Postman. It is located on the top panel, you fill in the username and password, and an authorization header will be generated.

+1
source share

All Articles