2

I want to consume a Google API within my web application. Specifically, I want to use Firebase to send Push-Messages to mobile devices.

I tried using Advanced REST Client for testing the functionality. When I configure it like this:

Request configured in a testing client

I get "200 OK" and the push message is displayed on the smartphone.

I tried implementing this in Visual Studio now:

        HttpClient client = new HttpClient();

        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send");
        requestMessage.Headers.Add("Content-Type", "application/json");
        requestMessage.Headers.Add("Authorization", "key=AAAAG...:APA91bH7U...");
        requestMessage.Content = new StringContent("{ \"notification\": { \"title\": \"Portugal vs. Denmark\", \"body\": \"5 to 1\"  }, \"to\": \"caJ_lIocLY4:APA91bEL19TRFFPlXFx3kZ_FTt...Q\" }");

HttpResponseMessage response = client.SendAsync(requestMessage).GetAwaiter().GetResult();

But get Exceptions like System.InvalidOperationException on the first requestMessage.Headers.Add line. The Message of the Exception is (roughly translated) "Wrongly used Headername. Make sure, that Requesetheaders are usedi with "HttpRequestMessage"-Objects, Answerheaders with "HttpREsponseMessage"-Objects[...].

For me, what I'm doing there is a POST Message. I'm calling the service and I need to pass arguments. So for me this is a request with arguments.

What do I need to do here?

1 Answer 1

2

The Content-Type is defined using the content property. In your example you are using a StringContent. Add encoding and Content-Type to the StringContent class instead.

HttpClient client = new HttpClient();

HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send");
requestMessage.Headers.Add("Authorization", "key=AAAAG...:APA91bH7U...");
requestMessage.Content = new StringContent(jsonAsStringContent, Encoding.UTF8, "application/json");

HttpResponseMessage response = client.SendAsync(requestMessage).GetAwaiter().GetResult();
Sign up to request clarification or add additional context in comments.

3 Comments

This helped, thanks. But I have to add that I also needed to use Headers.TryAddWithoutValidation(); on the third line because for some reason the huge long key is not accepted as valid by the framework else. Thank you!
i have a cloud database in vuforia, it gives web services for accessing its storage. While I make an Http post request using the HttpRequestMessage it returns a 404 not found error even though resource exists.I did it using the above code. im new to this Csharp so if you could help me it would be really thankful
I would suggest that you create a new question, where you can elaborate on your issue and bring some context to your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.