question

justindooley avatar image
justindooley asked

CloudScript http.request forcing content-type: application/json; charset=utf-8

When making a put request to sendgrid I get an error saying

invalid content-type: application/json; charset=utf-8

Is there a way to prevent the content type from including charset=utf-8

Here is a script that I use to reproduce this

var request =	{
        "contacts": [
        {
            "email": profile.PlayerProfile.ContactEmailAddresses[0].EmailAddress,
            "first_name": "Undefined",
            "last_name": "Undefined",
            "custom_fields": {
                "e6_T": "False",
		"e1_T": "Undefined",
                "e3_T": "Undefined",
                "e2_T": "Undefined",
                "e4_T": "Undefined"
            }
        }]
    };
    
    var url = "https://api.sendgrid.com/v3/marketing/contacts";
    var method = "PUT";
    var headers = {};
    headers['authorization'] = "MY_SECRET_KEY_HERE";
    
    var EmailResponse = http.request(url,method,JSON.stringify(request), "application/json",headers);

As an alternative, is there a way of sending these requests with a XMLHttpRequest() instead?

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

PlayFab http.Request follows the HTTP standards. It will set the charset to utf-8 by default. You could set the fourth parameter to "application/json;charset=xxx" at line 22 with the charset you need and try again. If this issue still happens, you may need to ask for support from SendGrid.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

justindooley avatar image justindooley commented ·

Spoke with elevated support at Send Grid. They said that sending a different charset will result in an error and would like to know playfab is using a send grid library. If so, which one?

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao justindooley commented ·

PlayFab is not using any SendGrid library. If there is no “charset” set, PlayFab Cloud Script will by default use utf-8. You are able to specify the charset you need as I’ve said. If you’d like no “charset”, please feel free to post a thread on Feature Requests forum.

0 Likes 0 ·
JayZuo avatar image JayZuo ♦ justindooley commented ·

It seems SendGrid does not accept "charset" content heard for some APIs as Contacts API fails with Content-Type application/json; charset=utf-8 Issue #910 sendgrid/sendgrid-csharp (github.com). However, legacy Cloud Script will always add "charset" content heard. To solve this problem, we'd suggest using CloudScript using Azure Functions.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.