Hello team,
I am trying to create a developer by POSTing information to the server. I am using wordpress which has a special function for that (wp_remote_post).
Bellow is the code I wrote and the given respone. The question is how I need to present the data I want to post to Apigee. I think the problem is related with the representation of the data.
Any help will be much apriciated!
//CODE $url= 'https://api.enterprise.apigee.com/v1/organizations/vaninayordanova-trial/developers'; $response = wp_remote_post('https://api.enterprise.apigee.com/v1/organizations/vaninayordanova-trial/developers', array( 'headers' => array('Authorization'=> 'Basic XXXXXXX' ,'Content-Type' => 'application/json'), 'body' => array( 'email'=> 'emailZZZZZ@abv.bg' ))); //ERROr https://github.com/apigee/edge-php-sdk
Solved! Go to Solution.
It should be simple, See Apigee Create Developer API here,
curl -X POST \ https://api.enterprise.apigee.com/v1/organizations/vaninayordanova-trial/developers \ -H 'authorization: Basic XXXXXXXXX' \ -d '{ "email" : "developer_email@example.com", "firstName" : "first_name", "lastName" : "last_name", "userName" : "user_name" }'
More about the WP Function to make post call here,
$post_url ='https://api.enterprise.apigee.com/v1/organizations/vaninayordanova-trial/developers'; $arg_data = array('email'=> "developer_email@example.com", "firstName" : "first_name", "lastName" : "last_name", "userName" : "user_name");
$data = json_encode( $arg_data ); $args = array('headers'=> array('Content-Type'=>'application/json', 'Authorization' => 'Basic XXXXXXXXXXX'),'body'=> $data );
$response = wp_remote_post( esc_url_raw( $post_url ), $args ); $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response );
Check the response code for errors:
// Bail out early if there are any errors
if(!in_array( $response_code, array(200,201))|| is_wp_error( $response_body )) returnfalse;
Since the response is in JSON, I prefer to convert it to PHP, then returning the result.
$result = json_decode( $response_body );
[body] => { "message" : "Unexpected character ('e' (code 101)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@fd96878; line: 1, column: 2]", "contexts" : [ ] } [response] => Array ( [code] => 400 [message] => Bad Request ) [cookies] => Array ( )
Note : Never post credentials in public form. I have updated question masked the credentials in header.
It should be simple, See Apigee Create Developer API here,
curl -X POST \ https://api.enterprise.apigee.com/v1/organizations/vaninayordanova-trial/developers \ -H 'authorization: Basic XXXXXXXXX' \ -d '{ "email" : "developer_email@example.com", "firstName" : "first_name", "lastName" : "last_name", "userName" : "user_name" }'
More about the WP Function to make post call here,
$post_url ='https://api.enterprise.apigee.com/v1/organizations/vaninayordanova-trial/developers'; $arg_data = array('email'=> "developer_email@example.com", "firstName" : "first_name", "lastName" : "last_name", "userName" : "user_name");
$data = json_encode( $arg_data ); $args = array('headers'=> array('Content-Type'=>'application/json', 'Authorization' => 'Basic XXXXXXXXXXX'),'body'=> $data );
$response = wp_remote_post( esc_url_raw( $post_url ), $args ); $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response );
Check the response code for errors:
// Bail out early if there are any errors
if(!in_array( $response_code, array(200,201))|| is_wp_error( $response_body )) returnfalse;
Since the response is in JSON, I prefer to convert it to PHP, then returning the result.
$result = json_decode( $response_body );
I did it, it works!!! Thank you 🙂
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |