Title: Guzzle post request
Last modified: December 24, 2022

---

# Guzzle post request

 *  Resolved [labyrinthman](https://wordpress.org/support/users/labyrinthman/)
 * (@labyrinthman)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/)
 * Hello,
 * I’m using Guzzle for my WP Rest application.
 *     ```wp-block-code
        "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
 *     ```wp-block-code
       $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)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16318630)
 * What method are you using for authentication?
 *  Thread Starter [labyrinthman](https://wordpress.org/support/users/labyrinthman/)
 * (@labyrinthman)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16318972)
 * Hello [@otto42](https://wordpress.org/support/users/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.
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16319282)
 * There is no authentication at all in your code above. Did you add it to the Guzzle
   request?
 *  Thread Starter [labyrinthman](https://wordpress.org/support/users/labyrinthman/)
 * (@labyrinthman)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16319337)
 * [@threadi](https://wordpress.org/support/users/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.
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16319338)
 * 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)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16319396)
 *  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](https://wordpress.org/support/users/labyrinthman/)
 * (@labyrinthman)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16319512)
 * Thanks [@otto42](https://wordpress.org/support/users/otto42/) and [@threadi](https://wordpress.org/support/users/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.
 *     ```wp-block-code
       $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.

## Tags

 * [guzzle](https://wordpress.org/support/topic-tag/guzzle/)
 * [postman](https://wordpress.org/support/topic-tag/postman/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 3 participants
 * Last reply from: [labyrinthman](https://wordpress.org/support/users/labyrinthman/)
 * Last activity: [3 years, 5 months ago](https://wordpress.org/support/topic/guzzle-post-request/#post-16319512)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
