Skip to content

Invoke-RestMethod Post unicode body data will become a question mark #17872

Closed

Description

@kkbruce

Prerequisites

Steps to reproduce

Run this script, body includes Unicode text:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")

$body = "{
`n    `"Hello`":`"測試`"
`n}"

$response = Invoke-RestMethod 'https://eob3ypv44n7x2sm.m.pipedream.net' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Expected behavior

Get unicode text in body data.

Actual behavior

get two ?? data.

Error details

No response

Environment data

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.5
PSEdition                      Core
GitCommitId                    7.2.5
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

I use https://requestbin.com service to get the test results:

image

Activity

  1. 237dmitry commented on Aug 11, 2022

    @237dmitry

    Get unicode text in body data.

    There is not body property:

    {
      "about": "Pipedream is the fastest way to connects APIs. Build and run workflows with code-level control when you need it — and no code when you don't.",
      "event_id": "2DDHXkQxFcwKqHOBBIiPu6LboWi",
      "workflow_id": "p_JZCxaKP",
      "owner_id": "u_7bheL12",
      "deployment_id": "d_5ZsaMoG9",
      "timestamp": "2022-08-11T14:06:16.979Z",
      "inspect": "https://pipedream.com/@/p_JZCxaKP",
      "quickstart": "https://pipedream.com/quickstart/"
    }
    
  2. kkbruce commented on Aug 12, 2022

    @kkbruce
    Author

    237dmitry

    My negligence is that pipedream.com is a service that needs to log in to see the results.

    I change another service:

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Content-Type", "application/json")
    
    $body = "{
    `n    `"Hello`":`"您好`"
    `n}"
    
    $response = Invoke-RestMethod 'https://www.toptal.com/developers/postbin/1660271986467-6472524027340
    ' -Method 'POST' -Headers $headers -Body $body
    $response | ConvertTo-Json
    

    After running the script, you can check the results of POST at the following URL:

    https://www.toptal.com/developers/postbin/b/1660271986467-6472524027340

    like this:

    image

    Use PostBin to collect all requests to a special URL which you can use to test your API Clients or your WebHooks.
  3. 237dmitry commented on Aug 12, 2022

    @237dmitry

    I change another service

    404 - Not Found

  4. kkbruce commented on Aug 12, 2022

    @kkbruce
    Author

    237dmitry

    Sorry again. confirm https://www.toptal.com/developers/postbin/api#create-bin docs,

    This bin is value for 30 mins after which time it will cease to exist and all operations on it will fail.

    You can easily use https://www.toptal.com/developers/postbin/ service to create a test Url, then replace it to test code.

    I'm just looking for an easy place to reproduce the problem, but I think it's not the problem itself since I've attached pictures of the test results.

    Use the PostBin API to help you test requests to an API that isn't under your control or to add webhooks to your product.
    Use PostBin to collect all requests to a special URL which you can use to test your API Clients or your WebHooks.
  5. 237dmitry commented on Aug 12, 2022

    @237dmitry

    I confirm "Hello: ??"

    Win-11, Linux, pwsh 7.2.5

  6. kasini3000 commented on Aug 18, 2022

    @kasini3000

    It doesn't look like a bug, is it that you didn't encode the message?
    eg:

    $body = "xxx"
    $body2 = [System.Text.Encoding]::UTF8.GetBytes($body) 
    Invoke-RestMethod -Body $body2
  7. CarloToso commented on Oct 4, 2022

    @CarloToso
    Contributor

    You can solve this issue by editing the charset encoding like this:

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Content-Type", "application/json; charset=utf-8")
  8. CarloToso commented on Feb 14, 2023

    @CarloToso
    Contributor

    Ilya (@iSazonov) Fixed in 7.4 preview1
    Test:

    $body = @{Hello='您好'}|ConvertTo-Json
    $response = Invoke-WebRequest 'https://www.httpbin.org/post' -Method 'POST' -Body $body
  9. ghost closed this as completedon Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions