I'm trying to retrieve available options for shipping method for sales order form but I encountered an unexpected error in using getSelectOptions.
function get_select_options(datain)
{
var rec = nlapiCreateRecord(datain.recordType);
var field = rec.getField(datain.field);
var options = field.getSelectOptions();
return options;
}
And here's my code for calling the script.
$url = "https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl";
$client = new Client();
$authorization = [
'NLAuth nlauth_account='.getenv('NETSUITE_ACCOUNT'),
'nlauth_email='.getenv('NETSUITE_EMAIL'),
'nlauth_signature='.getenv('NETSUITE_PASSWORD'),
'nlauth_role='.getenv('NETSUITE_ROLE')
];
$response = $client->request('GET', $url,
'headers' => [
'Authorization' => implode(',', $authorization),
'Content-Type' => 'application/json'
],
'query' => [
'script' => '343',
'deploy' => '1',
'recordType' => 'salesorder',
'field' => 'shipmethod'
]
]);
return json_decode($response->getBody());
My record type and field seems to be correct as what I have read in the docs. I also followed some of the example codes that uses getSelectOptions, but I can't move on because of some unexpected/unknown error. I also tried to return only the record (rec) just to make sure that my PHP code is working.
GuzzleHttp\Exception\ServerException with message
'Server error: `GET https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl?script=343&deploy=1&recordType=salesorder&field=shipmethod`
resulted in a `500 Internal Server Error` response:
{"error" : {"code" : "UNEXPECTED_ERROR", "message" : "An unexpected error has occurred. Error ID: irc3j6d915k2asvrqbfhb" (truncated...) '
Related
I was working on the Quickbooks API for one of my projects. I'm getting the following error:
GuzzleHttp\Exception\ClientException Client error: POST https://sandbox-quickbooks.api.intuit.com//*Private Info */ resulted in a 400 Bad Request response: {"Fault":{"Error":[{"Message":"Request has invalid or unsupported property","Detail":"Property Name:Unrecognized field \ (truncated...)
The Code:
I have no idea what it is saying to me to fix it. Can anyone help me with this?
Please find the updated code
if(!$buyer->details->quickbooks_id){
$customer = $http->post(
// Sandbox API hidden for security reason
[
'headers' => [
'Accept'=> 'application/json',
'Content-type'=> 'application/json',
'Authorization'=> 'Bearer '.$oauth['access_token']
],
'body' => json_encode(
[
'PrimaryEmailAddr' => [
'Address' => $buyer->email
],
'DisplayName' => $buyer->first_name.' '.$buyer->last_name.' - '.$buyer->details->business_name,
'PrimaryPhone'=>[
'FreeFormNumber'=> $buyer->phone
],
'CompanyName'=>$buyer->details->business_name,
'GivenName'=>$buyer->first_name,
'FamilyName'=>$buyer->last_name,
'BillAddr'=> [
"CountrySubDivisionCode" => $buyer->details->state,
"City" => $buyer->details->city,
"PostalCode" => $buyer->details->zip,
"Line1" => $buyer->details->address,
"Country" => $buyer->details->country
]
])
]
);
$customer = json_decode((string)$customer->getBody(), true);
$buyer->details->quickbooks_id = $customer['Customer']['Id'];
$buyer->details->save();
}
dd($buyer, $buyer->details, $buyer->details->quickbooks_id);
Probably the issue is with the request body. I had the same issue with the data i pass.
I have created an API request, it connects to the server fine and passes basic authentication. The error I receive is
"Uncaught GuzzleHttp\Exception\ClientException: Client error: POST http://my.api.com/dest/addr resulted in a 400 Bad Request response:
{"code":400,"result":"Missing customer id"
I have tried many ways to pass the query parameters and none of them have worked. I checked to make sure the JSON being output was formatted correctly. I have followed the directions in the Guzzle documentation. I have also tried every solution I could find in the forums and none of them gave me a different response. I am not sure what I am missing, hoping someone can see what the problem might be... I have pasted what I feel is the code that best follows the Guzzle documentation below. Thanks.
<?php
require __DIR__.'/../vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
Use GuzzleHttp\Psr7;
try {
$client = new Client(['base_uri' => 'http://my.api.com/']);
$credentials = base64_encode('12345');
$request_param = [
'json' => [
'customer' => [
'name' => 'Mary',
'id' => 111
],
'products' => [
[
'product_id' => 2,
'qty' => 3
]
]
]
];
$request_data = json_encode($request_param, true);
$headers = ['headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . $credentials
]];
$response = $client->request('POST', 'dest/addr', $headers, $request_data);
echo $response->getStatusCode();
echo "<br />";
echo $response->getHeader('content-type')[0];
echo "<br />";
echo "<pre />";
echo $response->getBody()->getContents();
}
catch(\GuzzleHttp\Exception\ClientException $e) {
echo $e->getCode(). '<hr />';
echo $e->getMessage();
}
//Server Exception
catch(\GuzzleHttp\Exception\ServerException $e) {
echo $e->getCode(). '<hr />';
echo $e->getMessage();
}
if your my.api.com can accept json request(not form params json=json_string&something=value in post body).you could try like this
$json_data = [
'customer' => [
'name' => 'Mary',
'id' => 111
],
'products' => [
[
'product_id' => 2,
'qty' => 3
]
]
];
$headers = [
'Authorization' => 'Basic ' . $credentials
];
$response = $client->request('POST', 'dest/addr', [
'json' => $json_data,
'headers' => $headers
]);
Have you tried using the json request option like you did the headers request option? The documentation shows an example like so:
An easy way to upload JSON data and set the appropriate header is using the json request option:
$r = $client->request('PUT', 'http://httpbin.org/put', [
'json' => ['foo' => 'bar']
]);
The syntax is very similar to the headers option. At the moment, your code is trying to send the raw json. Documentation for json request option found at https://docs.guzzlephp.org/en/stable/quickstart.html#uploading-data
I am doing a post request to an API, When I use postman, everything works fine as expected and data is returned. When I use Guzzler, cURL or file_get_contents(), I get this long error that the incoming message has an unexpected format 'Raw'.
My search on the net suggested that I use Content-Type: application/JSON but that doesn't solve it in my case.
This is my code:
$user =auth()->user();
$postRequest = array(
'userId' => $user->id,
'username' => $user->username,
'currency'=> 'TRY',
'nick'=>$user->username
);
$tvbetURL ="https://somewebsite.com/set_user_data";
$headers = [
'token' => '=EDMwU2N2MjM0kDNiVjYiFskljbgkjgerHUIHIOFG',
'Content-Type' => 'application/json ',
];
$client = new GuzzleClient([
'headers' => $headers
]);
$r = $client->request('POST', $tvbetURL, [
'form_params' =>$postRequest
]);
$response = $r->getBody()->getContents();
dd($response);
THE ERROR:
[2020-08-28 07:05:51] production.ERROR: Server error: POST https://somewebsite.com/set_user_data resulted in a 500 Internal Server Error response:
The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', ' (truncated...)
{"userId":1,"exception":"[object] (GuzzleHttp\Exception\ServerException(code: 500): Server error: POST https://somewebsite.com/set_user_data resulted in a 500 Internal Server Error response:
The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', ' (truncated...)
at
/opt/lampp/htdocs/projects/freelance/git/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113)
[stacktrace]
Thank you i advance.
I try to send the file via GuzzleHTTP from my application to external API, I make it like this:
public function storeImagesInAmazon(Request $request) {
$uploadFilePath = 'some/endpoint';
$file = $request->file('file');
$client = new Client();
$response = $client->request('POST', $uploadFilePath, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'multipart/form-data',
],
'multipart' => [
[
'name' => 'file',
'contents' => $file
]
]
]);
$result = json_decode($response->getBody(), true);
return [
'hashedID' => $result['hashedID']
];
}
The error I get is:
Server error: POST some/endpoint resulted in a 500 Internal Server Error response:\n
Errorerror while processing file: Failed to process file: part was null
I tested it via Postman, adding key = 'file', value = 'some_file.pdf' in Body form-data, I am sure file is correct, I mean it isn't damaged, I tried to post different files large one, a small one, pdf or jpg/png.
But I still have this issue and I can't find a solution for this.
I found this solution Guzzle form_params not accepting array
what I'm trying to say is, you need $option as your next param like in that post
$response = $client->post('api', $options);
and that $option is where your headers or multipart or other options goes as per documentation. i already tried using $options and its worked in my case.
I am sending a POST request to the following resource and getting a 400. I understand what the error means, but still am unsure why I'm getting it when a GET request to the same resource works.
/lists/{list_id}/members
Here is a exerpt of the code:
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', // <-- Drop in a GET here and it works, other than it's not the behavior I need.
env('MAILCHIMP_API_URL') . 'lists/' . env('MAILCHIMP_LIST_KEY') . '/members',
[
'auth' => ['app', env('MAILCHIMP_API_KEY')],
'query' => [
'email_address' => 'donnie#test.com',
'email_type' => 'html',
'status' => 'subscribed',
]
]);
dd($response->getStatusCode());
Response
Client error: `POST https://XXXX.api.mailchimp.com/3.0/lists/XXXX/members?email_address=donnie%40test.com&email_type=html&status=subscribed`
resulted in a `400 Bad Request`
response: {
"type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title": "Invalid Resource",
"status": 400,
"detail": "The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
"instance": "f32e7076-b970-4f5c-82c6-eec5875e83b4",
"errors": [{
"field": "",
"message": "Schema describes object, NULL found instead"
}]
}
You are sending a POST request with query parameters. You need to send JSON encoded body!
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', // <-- Drop in a GET here and it works, other than it's not the behavior I need.
env('MAILCHIMP_API_URL') . 'lists/' . env('MAILCHIMP_LIST_KEY') . '/members',
[
'auth' => ['app', env('MAILCHIMP_API_KEY')],
'json' => [
'email_address' => 'donnie#test.com',
'email_type' => 'html',
'status' => 'subscribed',
]
]);
dd($response->getStatusCode());