title: Translating Curl request to Invoke-WebRequest with --data-urlencode parameter
body: <p>I'm trying to request data from Mixpanel via there API. </p>
  
  <p><a href="https://developer.mixpanel.com/docs/jql-getting-started" rel="nofollow noreferrer">Mixpanel JQL Getting Started</a></p>
  
  <p>I can run a modified version of their basic example script and receive data.</p>
  
  <pre><code># sends the JQL code in `query.js` to the api
  # to a project with sample data for this tutorial
  curl https://mixpanel.com/api/2.0/jql \
      -u API_SECRET:PASSWORD \
      --data-urlencode script@query.js
  </code></pre>
  
  <p><code>query.js</code> above is the query payload, which contains a JS function that is run on Mixpanel servers. The request returns JSON data.</p>
  
  <p>I have been unable to translate this to Invoke-WebRequest. Below is my attempt.</p>
  
  <pre><code>$urlEncodedString = gc query.js | Out-String | % {[System.Web.HttpUtility]::UrlEncode($_)}
  $user = 'SECRET'; 
  $pass = 'PASSWORD';  
  $pair = "$($user):$($pass)";  
  $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair));
  $basicAuthValue = "Basic $encodedCreds";
  $Headers = @{ Authorization = $basicAuthValue };
  
  Invoke-WebRequest -Uri ("https://mixpanel.com/api/2.0/jql") -Headers $Headers -Method Post -Body "Script@$urlEncodedString
  </code></pre>
  
  <p>Returns a 400 from the server. 
  If I use verbose, the payload size is shown as -1 byte.</p>
  
  <p>The authentication is correct, but I'm sure I''m not placing the "script@$urlEncodedString" correctly.</p>
  
  <p>I've tried building the body as a dictionary <a href="https://stackoverflow.com/questions/54520054/curl-to-invoke-webrequest-command">according to this answer</a>, but no success either. I get the same error and message in verbose mode.</p>
  
  <p>How do I translate the --data-urlencode portion to Invoke-WebRequest?</p>