0

Hi I know that there are similar questions already in there but I have ruled them out.

So I am trying to make a simple POST method with curl and keep getting error 500. Have I missed something ?

// Get cURL resource
$curl = curl_init();

//POST request body
$post_data = array(
    'subscription_uuid' => $subscription_uuid,
    'merchant' => $merchant_id
);

echo "JSON in POSTFIELDS:" . json_encode($post_data, JSON_PRETTY_PRINT) . "\n";

// Set Headers, endpoint and option to output response as string
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://subscriptions-jwt.fortumo.io/subscriptions/cancel',
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => array(
        'Content Type: application/json' ,
        'Authorization: Bearer' . " " . $jwt

    ),
    CURLOPT_POSTFIELDS => json_encode($post_data)
));

// Send the request & save response
$unsubscribe_response = curl_exec($curl);

$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

//Request URL:
echo "Unsubscribe Request URL:\n" . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . "\n";
echo "Error Code:" . $statusCode . "\n";

And here is the response I get from that code block(simplified this using echo-s):

JSON in POSTFIELDS:{
    "subscription_uuid": "<<MY-TOKEN>>",
    "merchant": "<<MY-TOKEN>>"
}
Unsubscribe Request URL:
https://subscriptions-jwt.fortumo.io/subscriptions/cancel
Error Code:500

Wierd thing is that using exactly same set of headers, JSON postfields and URL in a tool such as Advanced REST client everything works fine and I get 200 response with no problem.

Something is wrong with my code. Can anyone please spot the issue? Thanks in advance!

wbdlc
  • 1,070
  • 1
  • 12
  • 34
  • 2
    The 500 is coming from the remote server, not this code, so it's hard to tell. If that is not your server, then you'll need to contact them to find out if it's a bug on their end or if it's something that you're not passing along (which really ought to give you a better error message) – aynber Dec 04 '18 at 15:09
  • but exactly same POST request when using some REST client works as intended. That's why I was wondering.. – Ott Jakovlev Dec 04 '18 at 15:13
  • 2
    It's hard to tell. But since you don't have the code that's giving the 500 error, you can't narrow down what the problem might be. A 500 is an extremely generic error message that gives absolutely no help. Contacting the owners of the remote server will hopefully allow them to look at their server error logs and your code to find out what the problem might be. – aynber Dec 04 '18 at 15:18
  • did a var_dump and got: `java.lang.IllegalArgumentException: Null charset name at java.nio.charset.Charset.lookup(Charset.java:457) at java.nio.charset.Charset.forName(Charset.java:528) at io.fortumo.utils.servlet.ServletInputStreamReader.readStreamToString(ServletInputStreamReader.java:58) at io.fortumo.verification.RequestWithPayloadBodyAuthFilter.getMerchantIdentifier(RequestWithPayloadBodyAuthFilter.java:79)` – Ott Jakovlev Dec 04 '18 at 15:35
  • I figure it does not tell us much... – Ott Jakovlev Dec 04 '18 at 15:36
  • 1
    Not really. It does say `Null charset name`, but I don't know if that's something you need to pass in as a header, or if it's something missing on their end. – aynber Dec 04 '18 at 15:40

0 Answers0