• Resolved labyrinthman

    (@labyrinthman)


    Hello,

    I’m using Guzzle for my WP Rest application.

     "guzzlehttp/guzzle": "^7.5"

    I can do the GET requests without any problems but when it comes to POST request I’m getting a

    401 Unauthorized response: {“code”:”rest_cannot_create”,”message”:”Sorry, you are not allowed to create posts as this user.”,”data”:{“status”:401}}

    However, when I’m doing the POST request using Postman I can make POST request without any problems

    $response = $client->request('POST', '/wp-json/wp/v2/our-cruises/', [
              'json' => [
                   'title' => 'This is a new test post from REST API',
                   'content' => 'This is a content from REST API'
                        ]
    ]);
     
     
     
    $body = $response->getBody();
    $arr_body = json_decode($body);
    print_r($arr_body);
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    What method are you using for authentication?

    Thread Starter labyrinthman

    (@labyrinthman)

    Hello @otto42 ,

    I’m using Application Password method. When I use Postman I can make a POST request, but when I use Guzzle itself then I get an error.

    There is no authentication at all in your code above. Did you add it to the Guzzle request?

    Thread Starter labyrinthman

    (@labyrinthman)

    @threadi yes I did.

    As I previously mentioned, I can do the POST request by using Postman but I can’t do it in my code. I have put my username and password in code but I don’t want to put it here on a public forum where anyone can see it.

    I assure you that I did that.

    Showing the whole code would make such queries to you unnecessary. Feel free to replace the credentials with x. Just by the structure of the code it should be clear what it is about and you could then see if there is a problem there.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    We can’t guess what’s wrong with your code without seeing the actual code. Feel free to eliminate the username and password, but not the fields showing how you’re doing it.

    Basically, a 401 means you’re not authorized. Which means the username and password didn’t work. So we kind of have to see what you’re doing to tell you what’s wrong with it.

    Thread Starter labyrinthman

    (@labyrinthman)

    Thanks @otto42 and @threadi
    I realize where I made an error. I was putting authentication in wrong place, I was putting it while I was instantiating Client and not where I’m making a request.
    Here is the code if somebody ever comes to the similar problem.

    $client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://website.com'
    ]);   
    
     
     
    $response = $client->request('POST', "/wp-json/wp/v2/posts", [
    	'json' => [
    		'acf' =>[
    			'acf_field_1' => 'value_1',
    			'acf_field_2'=>  'value_2'
    		]
    	],
    	"headers" => [
    		"Authorization" => "Basic ".base64_encode($username.":".$application_password)
    	] 
    ]);
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Guzzle post request’ is closed to new replies.