Error code 415 : Unsupported Media Type' - occures when: using API to cancel Paypal subscription

PeterKhaled
Contributor
Contributor

Dear Community,

 

I have spent the whole day trying to cancel a Paypal subscription by code. All the time I keep geting the response: Error 415 Unsupported Media Type. I have turned google up and down to find a solution, uselessly. I am using the follwoing C#.Net code:

 

string url = "https://api-m.paypal.com/v1/billing/subscriptions/" + subscription_ID + "/cancel";

var client = new HttpClient();
var msg = new HttpRequestMessage(HttpMethod.Post, url);
msg.Headers.Authorization = new AuthenticationHeaderValue("Bearer", txt.Text);
msg.Content = new StringContent(JsonConvert.SerializeObject("reason: Item out of stock"));// I have tried either the not using of this convert uselessly
var response = client.SendAsync(msg).Result;

---

 

Kindly, what to do ?

 

Thanks,

Peter

Login to Me Too
3 REPLIES 3

MTS_Jennifer
Moderator
Moderator

Hi @PeterKhaled 

Thank you for posting to the PayPal Community.

I noticed in your request new AuthenticationHeaderValue("Bearer", txt.Text);

This file type: txt.Text is most likely causing the 415 error message. Our server will not recognize a .txt file, so if you are writing your bearer token to a .txt file, it will not be read by the PayPal server. Please pass your token in the request, not as a file. 

 

Thank you,

Jennifer

MTS

PayPal

Login to Me Too

PeterKhaled
Contributor
Contributor
Dear Jennifer, Sorry i did not clarify this part. The txt.text, which I used, represents a string type (of a Textbox), not a file. Please note, it is successful when i apply my data directly by the postman. However, it fails by the .net code. Regards Peter
Login to Me Too

PeterKhaled
Contributor
Contributor

Kindly, im still stuck here. it supoose to be very easy to do !

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.