简体   繁体   English

C#Console Rest API标头有效负载

[英]C# Console Rest API header payload

I need to access a Portal which is controlled with Bearer Authentication. 我需要访问一个由Bearer身份验证控制的门户。 Client needs to obtain authentication token then add it to each request 客户端需要获取身份验证令牌,然后将其添加到每个请求中

URL ~/token Method POST Content-Type application/x-www-form-urlencoded Payload grant_type=password&username=UserName&password=Password URL〜/令牌方法POST内容类型应用程序/ x-www-form-urlencoded有效负载grant_type = password&username = UserName&password = Password

**How do i add the payload which includes the grant type and username and password to my code ** **如何将包含授权类型,用户名和密码的有效负载添加到我的代码中**

so far my code is as follows : 到目前为止,我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

 namespace ConsoleRestClient
 {
     class Program
    {
    static void Main(string[] args)
    {
        string URI = "https://token";
        WebRequest request = WebRequest.Create(URI);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

       }
     }`
   }

You should be sending your request with content type "application/json" and put requested login variable on the body. 您应该以内容类型“ application / json”发送请求,并将请求的登录变量放在正文中。 To give you an idea: 给您一个想法:

  • Headers: 标头:

    Content-Type:application/json 内容类型:application / json

  • Body: 身体:

    { "username": "MyUserName", "password": "MyPassword" } {“ username”:“ MyUserName”,“ password”:“ MyPassword”}

Probably you can try adding to the request headers, by doing something like this: 可能您可以尝试通过执行以下操作来添加到请求标头中:

string URI = "https://token";
WebRequest request = WebRequest.Create(URI);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.headers.Add(<header_name>, <header_value>);

then you read these headers on your api. 然后您会在api上阅读这些标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM