Curl -u equivalent in HTTP request

后端 未结 3 1777
执念已碎
执念已碎 2020-12-17 09:49

I\'ve been trying to plug into the Toggl API for a project, and their examples are all using CURL. I\'m trying to use the C# wrapper which causes a bad request when trying t

相关标签:
3条回答
  • 2020-12-17 10:34

    As explained to me in another post, you can pass the api token to the user property if you are using HttpWebRequest:

    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"my-secret-toggl-api-token:api_token")));
    
    0 讨论(0)
  • 2020-12-17 10:43

    The easy way is adding credential into url as user:pass@ format.

    https://my-secret-toggl-api-token:api_token@www.toggl.com/reports/api/v2/project/?page=...
            <---------------------------------->
    

    Alternately you can use credential with your http header like below:

    Authorization: Basic XXXXXX
    

    Here XXXXXX is base64(my-secret-toggl-api-token:api_token)

    0 讨论(0)
  • 2020-12-17 10:53

    Using fetch

    In node environment, your request might look something like so:

    const url = 'https://www.toggl.com/reports/api/v2/project/page=1&user_agent=devteam@example.com&workspace_id=1&project_id=2';
    const token = 'my-secret-toggl-api-token:api_token';
    
    fetch(url, {
      method: 'GET', // not required
      headers: {
        Authorization: `Basic ${Buffer.from(String(token)).toString('base64')}`,
      },
    })
    
    0 讨论(0)
提交回复
热议问题