我对卷发中的这种沉重的逃脱感到不舒服
curl -X POST -H "Content-Type:application/json" -d "{\"username\":\"john\",\"password\":\"12345\"}" http://localhost:8100/signup
我读了这个答案。不接受单引号
curl -X POST -H "Content-Type:application/json" -d '{"username":"john","password":"12345"}' http://localhost:8100/signup
org.springframework.core.codec.DecodingException:
JSON decoding error: Unexpected character (''' (code 39)):
expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
这也不起作用(例外是相同的)
curl -X POST -H "Content-Type:application/json" -d "{'username':'john','password':'12345'}" http://localhost:8100/signup
我的卷曲版本:
curl --version
curl 8.4.0 (Windows) libcurl/8.4.0 Schannel WinIDN
Release-Date: 2023-10-11
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS HSTS HTTPS-proxy IDN IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI threadsafe Unicode UnixSockets
在使用 JSON 有效负载(没有有效负载文件)执行卷曲时,我可以不用这种激进的转义吗?
我调试并发现,如果
com.fasterxml.jackson.core.json.UTF8StreamJsonParser
(字节数组)以撇号开头,则在 nextToken()
调用时从 _inputBuffer
引发异常
我尝试写一篇 MRE。这很棘手。该类有大量参数,其中一些参数很难实例化。这是我最好的尝试。它确实重现了错误,但由于我传递了空值和模拟,我无法确定我实际上模仿了实际情况。基本上,如果该方法在没有识别第一个字符的情况下到达末尾(它有很多 if 和 switch 情况),它会抛出该异常
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.json.UTF8StreamJsonParser;
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.mock;
public class GenericTest {
@Test
@SneakyThrows
void test() {
IOContext context = mock(IOContext.class);
int features = 65537;
InputStream inputStream = null;
ObjectCodec codec = null;
ByteQuadsCanonicalizer canonicalizer = null;
String input = "'{}'";
byte[] inputBuffer = input.getBytes();
int start = 0;
int end = input.length();
byte bytesPreProcessed = 0;
boolean bufferRecycleable = true;
UTF8StreamJsonParser jsonParser = new UTF8StreamJsonParser(
context, features, inputStream, codec,
canonicalizer, inputBuffer, start, end,
bytesPreProcessed, bufferRecycleable
);
assertThatCode(jsonParser::nextToken)
// the exception is translated and rethrown by Spring
.isInstanceOf(JsonParseException.class)
.hasMessageContaining("Unexpected character (''' (code 39))");
}
}
我尝试使用 Power Shell 而不是命令提示符。即使转义我也无法发出请求,无论我尝试什么,Shell都会抛出各种异常
尝试#1
curl -i -X POST -H "Content-Type:application/json" -d "{\"username\":\"john\",\"password\":\"12345\"}" http://localhost:8100/signup
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
尝试#2
curl -i -X POST -H Content-Type:application/json -d {"username":"john","password":"12345"} http://localhost:8100/signup
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
尝试#3
curl -i -X POST -H Content-Type:application/json -d {username:john,password:12345} http://localhost:8100/signup
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
尝试#4
curl -i -X POST -H Content-Type:application/json -d '{"username":"john","password":"12345"}' http://localhost:8100/signup
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
尝试#5
curl -i -X POST -H 'Content-Type:application/json' -d '{"username":"john","password":"12345"}' http://localhost:8100/signup
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
使用不同的 shell(不是 cmd.exe 或 PowerShell),它支持使用单引号来引用命令参数。例如,Bash 支持它。 Bash 可在 Windows 上使用,例如在 WSL2 和 MSYS2 中。在重击中:
curl -X POST -H "Content-Type:application/json" -d '{"username":"john","password":"12345"}' http://localhost:8100/signup