cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Extended Latin and 400 Bad Request

Extended Latin and 400 Bad Request

Vasili V.
Explorer | Level 4
Go to solution

Hello,

I try to create a new member with name that contains chars of extended Latin alphabet (Ñ, Í, Õ etc.). I use powershell Invoke-RestMethod command and generate a body on the fly.

I receive "The remote server returned an error: (400) Bad Request" response.

Do I have to somehow prepare my request before API calling or something else?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

Thanks! That's the error from the API, and it does indicate that the issue is that the contents of the request body couldn't be successfully decoded as JSON.

 

It sounds like the issue is with encoding these characters. That's generally done automatically, but that doesn't seem to be the case with PowerShell/Invoke-RestMethod.

 

I'm not a PowerShell expert, and it also looks like copying it onto the forum didn't work right here, but I believe you need to additionally encode the body like this:

 

$enc = [System.Text.Encoding]::UTF8
$body = $enc.GetBytes($body)

View solution in original post

10 Replies 10

Greg-DB
Dropbox Staff
Go to solution

Vasili, what's the content of the response body? It should contain a specific error message indicating the issue.

Vasili V.
Explorer | Level 4
Go to solution

Sorry for delay.

It is a part of the request's body:

{... "member_surname": "Cañas", "send_welcome_email": true ... }

The respponse is:
The remote server returned an error: (400) Bad Request

The same error for: León, Sofía etc. In other words any character from extended Latin alphabet causes this issue.

 

Greg-DB
Dropbox Staff
Go to solution

Thanks for following up Vasili. What you shared is just the status line of the response though. Can you retrieve and share the body of the response as well?

Based on your description of the problem, it's likely related to the encoding you're using for these characters. Please also share the code you're using to make the code so we can offer some help.

Thanks in advance! 

Vasili V.
Explorer | Level 4
Go to solution

Gregory,

The code:

$body = '{"member_email":"' + $member_email + '", "member_given_name": "' + $member_given_name +
'", "member_surname": "' + $member_surname + '", "member_external_id" : "' + $member_external_id +
'", "send_welcome_email": true }'

$newUser = Invoke-RestMethod -Uri https://api.dropbox.com/1/team/members/add -Body $body -ContentType application/json `
-Headers @{ Authorization = $token } -Method Post

It's powershell script. So I guess charset is set up by default.

What I am going to change is to create $body as JSON object, then use ConvertTo/From-Json and then add charset=utf-8 to -ContentType.

 p.s. I was not able to catch secured traffic using Fiddler, so I don't have body as an example.

Greg-DB
Dropbox Staff
Go to solution

I'm not too familiar with PowerShell, but it looks like you may be able to get the response body using the -OutFile option on Invoke-RestMethod.

Also, for reference, based on your code, you're using API v1, which is now deprecated. We recommend migrating to API v2 when you can. For example, the equivalent of /1/team/members/add is /2/team/members/add.

Anyway, "application/json" is the right Content-Type for these requests.

Vasili V.
Explorer | Level 4
Go to solution

So, we have migrated to api v2.0 and still have the same problem.

 

$new_members = @(

@{
member_email = $mail
member_given_name = $groupMember.givenName
member_surname = $groupMember.surname
member_external_id = $keyFromAD
send_welcome_email = $true
}
)
 
$body = @{
new_members = $new_members
} | ConvertTo-Json
 
As a result $body refers to json-like string presentation of members to be created. If the one contains any char from extended alphabet it causes the 400 Bad Request response.

 

Greg-DB
Dropbox Staff
Go to solution
For API v2, the first step for debugging failures like this is also to check the content of the response body. It should contain a more useful error message. Can you share the response body? Also, since this is likely related to how the request body is formatted, please share a sample of that as well. Thanks in advance!

Vasili V.
Explorer | Level 4
Go to solution

I captured a request and a response. I hide some sensitive info.

 

POST https://api.dropboxapi.com/2/team/members/add HTTP/1.1

 

{
"new_members": [
{
"member_email": "Baptiste.Bjaud@***",
"member_external_id": "ADSYNC ****",
"send_welcome_email": true,
"member_given_name": "Baptiste",
"member_surname": "B jaud"
}
]
}

 

HTTP/1.1 400 Bad Request

Error in call to API function "team/members/add": request body: could not decode input as JSON

 

The name is Béjaud.

 

 

Greg-DB
Dropbox Staff
Go to solution

Thanks! That's the error from the API, and it does indicate that the issue is that the contents of the request body couldn't be successfully decoded as JSON.

 

It sounds like the issue is with encoding these characters. That's generally done automatically, but that doesn't seem to be the case with PowerShell/Invoke-RestMethod.

 

I'm not a PowerShell expert, and it also looks like copying it onto the forum didn't work right here, but I believe you need to additionally encode the body like this:

 

$enc = [System.Text.Encoding]::UTF8
$body = $enc.GetBytes($body)
Need more support?