简体   繁体   English

ATLASSIAN JIRA C#REST POST

[英]ATLASSIAN JIRA C# REST POST

Im tying to use a REST post to send data from a MS SSIS Process: 打算使用REST帖子从MS SSIS进程发送数据:

    json = json + "{ ";
        json = json + "\"fields\": {";
        json = json + "\"project\": {  \"id\": \"XXX\" },";
        json = json + "\"summary\": \"REST ye merry gentlemen.\",";
        json = json + "\"description\": \"Hellow\",";
        json = json + "\"issuetype\": { \"name\": \"Bug\" }";
        json = json + "} ";
        json = json + "} ";


        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://xxx.atlassian.net/rest/api/2/issue/");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";


        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            MessageBox.Show(json);

            streamWriter.Write(json);
        }

        string mergedCredentials = string.Format("{0}:{1}", "xxx", "xxx");
        byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
        string credentials = Convert.ToBase64String(byteCredentials);

        //httpWebRequest.Headers.Add("Authorization", "Basic " + credentials);

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            //Now you have your response.
            //or false depending on information in the response

        } 

The server responds: 服务器响应:

SSIS package "Package.dtsx" starting. SSIS程序包“ Package.dtsx”开始。 Error: 0x1 at Script Task: System.Reflection.TargetInvocationException: Het doel van een aanroep heeft een uitzondering veroorzaakt. 错误:脚本任务:System.Reflection.TargetInvocationException处为0x1:Het doel van een aanroep heeft een uitzondering veroorzaakt。 ---> System.Net.WebException: De externe server heeft een fout geretourneerd: (400) Ongeldige opdracht. ---> System.Net.WebException:服务器外部故障发生的原因:(400)Ongeldige opdracht。 bij System.Net.HttpWebRequest.GetResponse() bij ST_8fbfe559ee824ef19f7c9bc2c425effc.csproj.ScriptMain.Main() --- Einde van intern uitzonderingsstackpad --- bij System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) bij System.Net.HttpWebRequest.GetResponse()bij ST_8fbfe559ee824ef19f7c9bc2c425effc.csproj.ScriptMain.Main()--- Einde van intern uitzonderingsstackpad --- bij System.RuntimeMethodHandle._InvokeMethodss(对象目标,Object []方法和属性,对象方法,属性[],属性,RuntimeTypeHandle typeOwner)bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder活页夹,Object []参数,CultureInfo文化,布尔skipVisibilityChecks)bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder活页夹,Object []参数,CultureInfo文化)
bij System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) bij Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript() Task failed: Script Task Warning: 0x80019002 at Package: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. bij System.RuntimeType.InvokeMember(字符串名称,BindingFlags bindingFlags,活页夹活页夹,对象目标,Object []提供的Args,ParameterModifier []修饰符,CultureInfo文化,String []命名为Params)bij Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine。 ExecuteScript()任务失败:脚本任务警告:程序包处的0x80019002:SSIS警告代码DTS_W_MAXIMUMERRORCOUNTREACHED。 The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); 执行方法成功,但是引发的错误数(1)达到允许的最大值(1); resulting in failure. 导致失败。 This occurs when the number of errors reaches the number specified in MaximumErrorCount. 错误数量达到MaximumErrorCount中指定的数量时,会发生这种情况。 Change the MaximumErrorCount or fix the errors. 更改MaximumErrorCount或修复错误。 SSIS package "Package.dtsx" finished: Failure. SSIS程序包“ Package.dtsx”已完成:失败。

The English bits of the error message are 错误消息的英文位是

The purpose of a call has caused an exception. 通话的目的引起了异常。 ---> System.Net.WebException: The remote server returned an error: (400) Bad Request ---> System.Net.WebException:远程服务器返回错误:(400)错误的请求

I tested your JSON with my application and it seems to be correct (assuming your projectId and issueTypeName are correct for your Jira). 我用我的应用程序测试了您的JSON,它似乎是正确的(假设您的projectId和issueTypeName对于您的Jira是正确的)。 ( EDIT2 : Make sure you send all the required fields for the issueType you're creating). EDIT2 :请确保发送您要创建的issueType的所有必填字段)。

But, why are you not adding Authorization in the header? 但是,为什么不在标题中添加授权?

Also, I encode my JSON to UTF8 before POSTing it. 另外,我在发布JSON之前将其JSON编码为UTF8。 Not sure if your StreamWriter do the same. 不知道您的StreamWriter是否也这样做。 ( EDIT1 : Ok, my apologies, StreamWriter Encode to UTF-8, but my code still differ with request.ContentLength and request.Accept, not sure again if it will make a difference) 编辑1 :好的,我很抱歉,将StreamWriter编码为UTF-8,但是我的代码仍然与request.ContentLength和request.Accept有所不同,请再次确认是否会有所不同)

byte[] data = Encoding.UTF8.GetBytes(postData);
request.Accept = "application/json";
request.ContentLength = data.Length;
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(data, 0, data.Length);
}

I could add all my Methods if this snippet doesn't help you. 如果此代码段无法帮助您,则可以添加所有方法。

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

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