Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [C#] Convertire una chiamata curl da JavaScript in C#

    Salve,
    potreste darmi una mano a convertire il codice JavaScript della chiamata POST che si trova nella funzione:

    [COLOR=var(--color-prettylights-syntax-keyword)]async[/COLOR][COLOR=var(--color-prettylights-syntax-keyword)]function[/COLOR][COLOR=var(--color-prettylights-syntax-entity)]ryte[/COLOR]({ useCaseId, inputContexts })

    https://github.com/rytr-me/documenta.../node/index.js

    In un codice C#, non riesco a trovare il modo giusto, non conosco molto JavaScript, io ho provato così:

    codice:
    
    
    codice:
    private void GetRytrContent(string _langID, string _toneID, string _usecaseID)
            {
                string api_key = "<MY-API-KEY>";
    
    
                WebRequest wr = WebRequest.Create("https://api.rytr.me/v1/ryte");
                wr.ContentType = "application/json";
                wr.Method = "POST";
                wr.Headers.Add("Authentication", "Bearer " + api_key);
    
    
                using (StreamWriter sw = new StreamWriter(wr.GetRequestStream()))
                {
                    string data = "{" +
    
    
                                    "\"languageId\":\"" + _langID + "\"," +
                                    "\"toneId\":\"" + _toneID + "\"," +
                                    "\"useCaseId\":\"" + _usecaseID + "\"," +
    
    
                                    "\"inputContexts\":\"{\"<USE-CASE CONTEXT-INPUT KEY-LABEL>\":\"<VALUE>\"}," +
    
    
                                    "\"variations\":\"1\"," +
    
    
                                    "\"userId\":\"<UNIQUE USER ID>\"," +
    
    
                                    "\"format\":\"html\"," +
                                    "\"creativityLevel\":\"default\"," +
    
    
                                    "}";
    
    
                    sw.Write(data);
                }
    
    
                WebResponse httpResponse = (WebResponse)wr.GetResponse(); //System.Net.WebException: 'Errore del server remoto: (400) Richiesta non valida.'
    
    
                using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
    
    
                    TxtDisplay.Text = result.ToString();
                }
            }


    ma mi da questo errore su
    WebResponse :

    //System.Net.WebException: 'Errore del server remoto: (400) Richiesta non valida.'

    Questa è la curl della richiesta:

    codice:
    curl \
      -H "Authentication: Bearer <API KEY>" \
      -H "Content-Type: application/json" \
      -d '{"languageId": "<LANUGAGE ID>", "toneId": "<TONE ID>", "useCaseId": "<USE-CASE ID>", "inputContexts": {"<USE-CASE CONTEXT-INPUT KEY-LABEL>": "<VALUE>"}, "variations": 1, "userId": "<UNIQUE USER ID>", "format": "html", "creativityLevel": "default"}' \
      -X POST https://api.rytr.me/v1/ryte

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887
    beh WebRequest è deprecata nei nuovi FW, usa HttpClient (consiglio)

    guarda questa famosa risposta
    https://stackoverflow.com/questions/...st-web-request

  3. #3
    Quote Originariamente inviata da djciko Visualizza il messaggio
    beh WebRequest è deprecata nei nuovi FW, usa HttpClient (consiglio)

    guarda questa famosa risposta
    https://stackoverflow.com/questions/...st-web-request
    Ho dato un occhiata e lo modificherò in quel modo come mi consigli tu, nel frattempo avevo fatto delle modifiche:

    Ho creato la stringa json con "JsonConvert.SerializeObject", e ho aggiunto il campo "inputContexts " che avevo omesso, anche se lo avevo incollato qui nel codice.

    Ora mi restituisce questo messaggio:
    {"success":false,"message":"Sorry, we were unable to process your request. Please try again."}



    Sono dubbioso se sto sbagliando nel creare il campo:
    inputContexts = "{'INTEREST_LABEL':'Ti propongo un nuovo business eccezionale'}",


    oppure altro..., nella documentazione non è spiegato come formattarlo e con quali dati, sono risalito un po tramite l'esempio JavaScript, ma possibile che sbaglio, questo è quello che chiede:


    "inputContexts": {"<USE-CASE CONTEXT-INPUT KEY-LABEL>": "<VALUE>"}


    Ma cosa è: <USE-CASE CONTEXT-INPUT KEY-LABEL> ???
    Presumo che: <VALUE> sua la frase dal cui estrapola le keywords per poi creare il post, quindi io ho inserito questo come testo: "Ti propongo un nuovo business eccezionale!".

    Inoltre non so se è giusto il campo "INTEREST_LABEL".

    Ho provato a cercare esempi in C# su Google per queste API di Rytr, ma non trovo nulla!!




    codice:
        public class RytrData
        {
            public string languageId { get; set; }
            public string toneId { get; set; }
            public string useCaseId { get; set; }
            public string inputContexts { get; set; }
            public string variations { get; set; }
            public string userId { get; set; }
            public string format { get; set; }
            public string creativityLevel { get; set; }
        }
    
    
            private void GetRytrContent(string _apyKey, string _langID, string _toneID, string _usecaseID)
            {
                WebRequest wr = WebRequest.Create("https://api.rytr.me/v1/ryte");
                wr.ContentType = "application/json";
                wr.Method = "POST";
                wr.Headers.Add("Authentication", "Bearer " + _apyKey);
    
    
                RytrData RytrData = new RytrData
                {
                    languageId = _langID,
                    toneId = _toneID,
                    useCaseId = _usecaseID,
                    inputContexts = "{'INTEREST_LABEL':'Ti propongo un nuovo business eccezionale!'}",
                    variations = "1",
                    userId = "USER1",
                    format = "text",
                    creativityLevel = "default"
                };
                var json = JsonConvert.SerializeObject(RytrData, Formatting.Indented);
    
    
    
    
                using (StreamWriter sw = new StreamWriter(wr.GetRequestStream()))
                {
                    string data = json.ToString();
    
    
                    sw.Write(data);
                }
    
    
                WebResponse httpResponse = (WebResponse)wr.GetResponse();
    
    
                using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
    
    
                    TxtDisplay.Text = result.ToString(); //{"success":false,"message":"Sorry, we were unable to process your request. Please try again."}
                }
            }
    Ultima modifica di w_t; 13-03-2022 a 12:23

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.