2015-06-04 8 views
8

Nel servizio WCF (nuvola blu), desidero supportare JSON. Sto creando alcuni metodi di test per vedere se tutto funziona. Posso ottenere il GET chiama a lavorare, ma quando sto facendo un post con un parametro semplice sarò sempre ottenere:Richiesta POST JSON WCF, parametro stringa singola non vincolante e restituzione 400

The remote server returned an error: (400) Bad Request. 

Se io non mando un parametro, si eseguirà il metodo, ma con un valore nullo come parametro, ovviamente. Ho provato diversi formati di JSON e WebMessageBodyStyle, ma nessuno sembra funzionare.

Se cambio il tipo di parametro in Stream, ricevo i dati, ma devo deserializzare manualmente. Questo non dovrebbe essere necessario, giusto?

Interfaccia:

 [OperationContract] 
     [WebInvoke(UriTemplate = "Test", 
      Method = "POST", 
      BodyStyle = WebMessageBodyStyle.WrappedRequest, 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json)] 
     string Test(string data); 

Impl: cliente

 public string Test(string data) 
     {   
      return "result is " + data; 
     } 

prova:

  WebClient client = new WebClient(); 
      client.Headers["Content-type"] = "application/json"; 
      client.Encoding = System.Text.Encoding.UTF8; 
      string jsonInput = "{'data':'testvalue'}"; 
      string postResponse = client.UploadString(postUrl, jsonInput); 
      Console.WriteLine("post response: " + postResponse); 

risposta

8

La combinazione d'oro è stato quello di usare le virgolette nel codice JSON combinato con WebMessageBodyStyle.WrappedRequest.

lavoro JSON:

string jsonInput = "{\"data\":\"testvalue\"}"; 

Quando si imposta WebMessageBodyStyle a nudo, il seguente JSON funziona:

string jsonInput = "\"testvalue\""; 
Problemi correlati